<%-- 
    Document   : index
    Created on : 18-nov-2013, 11.35.34
    Author     : Ilo
--%>

<%@page import="it.evento.Users"%>
<%@page import="it.evento.BusinessLogic"%>
<%@page import="it.evento.Eventsfeedback"%>
<%@page import="it.evento.utils.genpage.Useful"%>
<%@page import="it.evento.utils.genpage.ArticleRow"%>
<%@page import="it.evento.utils.genpage.RowCell"%>
<%@page import="java.util.LinkedList"%>
<%@page import="it.evento.Events"%>
<%@page import="java.util.List"%>
<%@page import="it.evento.utils.genpage.Article"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    boolean isLoggedUser = false;
    Users loggedUser = null;
    
    if(session.getAttribute("loggedUser")!=null) {
        loggedUser = (Users)session.getAttribute("loggedUser");
        isLoggedUser = true;
    }
    else {
        isLoggedUser = false;
    }
%> 
<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
    <head>
        <title>Evento</title>

        <jsp:include page="html/metadata.jsp" flush="true" />
        <script lang="javascript">
            $(function() {
                $("#datepickerstart").datepicker();
                $("#datepickerend").datepicker();
                
                /* TYPE EVENT*/
                $.getJSON("Controller?type=getType", function(data) {
                    var items = [];
                    items.push("<option value='-1'>n.p.</option>");
                    $.each(data, function(key, val) {
                        items.push("<option value='" + val.id + "'>" + val.label + "</option>");
                    });

                    $("<select/>", {
                        name: "eventType",
                        html: items.join("")
                    }).appendTo("#eventTypeDiv");

                });
                /* /TYPE EVENT*/           
                
                /* LOCATION*/
                $.getJSON("Controller?type=getlocations", function(data) {
                    var items = [];
                    items.push("<option value='-1'>n.p.</option>");
                    $.each(data, function(key, val) {
                        items.push("<option value='" + val.id + "'>" + val.label + "</option>");
                    });

                    $("<select/>", {
                        name: "eventLoc",
                        html: items.join("")
                    }).appendTo("#eventLocDiv");
                });
                /* /LOCATION*/
                
                
                /* Manage Stars */
                function manageStarsClick(score, evt) {
                    var self = $(this);
                    var eid = this.id.split("-")[1];
                    var votes = parseInt($('#votesfor-'+eid).html().split(" ")[0]);
                    var oldScore = self.attr('data-score');
                    
                    $.post("Controller",
                           {type: "eventStarsRate",
                            eventid: eid,
                            score: score},
                           function(data) {                               
                               if(data.status === 0) {
                                   votes++;
                                   self.raty('score', data.score).attr('data-score', data.score);
                                   $('#votesfor-'+eid).html(votes +  " vot" + (votes===1 ? "o" : "i") + ".");
                               }
                               else {
                                   self.raty('score', oldScore);
                                   $('#modal-window .modal-title').html("ERRORE");
                                   $('#modal-window #modal-message').html(data.message);
                                   $('#modal-window').modal('show');
                               }
                           },
                           'json');
                }
                
                RATY_DEFAULT_OPTIONS = {
                                        path: 'images/raty/',
                                        half: false,
                                        space: false,
                                        hints: ['Brutto', 'Scarso', 'Normale', 'Buono', 'Ottimo'],
                                        readOnly: <%= isLoggedUser ? false : true %>,
                                        score: function() {
                                          return $(this).attr('data-score');
                                        },
                                        click: manageStarsClick
                                       };
                                       
                $('[id^=starsfor]').raty(RATY_DEFAULT_OPTIONS);
                /* /Manage Stars */
                
            });
        </script>
        
    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" flush="true" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <%=
                                    new Article()
                                       .setTitle("Ricerca avanzata")
                                       .setContent("<form action='Controller?type=advancedSearchEvent' method='POST'> "
                                                    + "Categoria"
                                                    + "<div id='eventTypeDiv'></div>"
                                                    + "Location"
                                                    + "<div id='eventLocDiv'></div>"
                                                    + "Data di inizio"
                                                    + "<br>"
                                                    + "<input type='text' name='eventStartDate' id='datepickerstart'/>"
                                                    + " <br>"
                                                    + "Data di fine"
                                                    + "<br>"
                                                    + "<input type='text' name='eventEndDate' id='datepickerend'/>"
                                                    + "<br>"
                                                    + "Ricerca una parola"
                                                    + " <br>"
                                                    + "<input type='text' id='matchBox' name='matchBox'/>"
                                                    + "<br>"
                                                    + "<input type='submit'  id='searchBtn' value='Cerca' class='evento-button' />"
                                                    + "</form>")
                                       .print()
                                     %>
                                 
                                <!-- CARICAMENTO EVENTI DI UN TIPO -->
                                <% 
                                List<Events> listEvents=(List<Events>) request.getAttribute("eventsList");
                                BusinessLogic businessLogic = new BusinessLogic();
                                if(listEvents!=null){ 
                                    String htmlcontent="";
                                    LinkedList<RowCell> cellsList = new LinkedList<RowCell>();
                                    
                                    if(listEvents.isEmpty()) {
                                        htmlcontent += "Non ci sono Eventi di questo tipo.";
                                    }
                                    else {
                                        for(int k=0; k<listEvents.size(); k++) {
                                            Events e = listEvents.get(k);
System.out.println(">>>>> " + e.getId());
//System.out.println(">>>>>>>> " + businessLogic.getEventsFeedback(e.getId()));
                                            //List<Eventsfeedback> lstStars = businessLogic.getEventsFeedback(e.getId());

                                            RowCell cell = new RowCell()
                                                            .setTitle(e.getName())
                                                            .setContent(Useful.cutText(e.getDescription()))
                                                          //  .setStars(Useful.calculateStars(lstStars))
                                                          //  .setRatesNumber(lstStars.size())
                                                            .setID(""+e.getId())
                                                            .setLink("Controller?type=showEvent&ide=" + e.getId());

                                            if(k!=0 && k%3==0) {
                                                htmlcontent += new ArticleRow().setCells(cellsList).print();
                                                cellsList.clear();
                                            }

                                            cellsList.add(cell);
                                        }
                                        
                                        htmlcontent += new ArticleRow().setCells(cellsList).print();
                                    }
                                    
                                 %>
                                    <%=
                                    new Article()
                                       .setTitle("Eventi Selezionati")
                                       .setContent(htmlcontent)
                                       .print()
                                     %>
                                <%}%>
                                
                                <!-- Stampa l'evento inviato dal controller -->
                                <%if(request.getAttribute("content")!=null){%>
                                    <%= request.getAttribute("content") %>
                                <%}%>
                            </div>
                            
                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" flush="true" />
                            <!-- /sidebar -->
                            
                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
    </body>
</html>
